<template>
  <div class="allArticle">
    <div class="search-allArticle">
      <el-form
        :inline="true"
        :model="formInline"
        class="demo-form-inline"
        ref="formInline"
      >
        <el-form-item label="标题" prop="title">
          <el-input
            v-model="formInline.title"
            placeholder="请输入文章标题"
          ></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="state">
          <el-select v-model="formInline.state">
            <el-option label="已发布" value="已发布"></el-option>
            <el-option label="草稿" value="草稿"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="分类" prop="type">
          <el-select v-model="formInline.type">
            <el-option
              v-for="item in classifyList"
              :key="item.id"
              :label="item.name"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item class="operate">
          <el-button type="primary" @click="onSubmit()">查询</el-button>
          <el-button @click="resetForm('formInline')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="table-allArticle">
      <!-- 头部 -->
      <div class="table-header">
        <el-button type="primary" @click="toEditor()">新建</el-button>
        <el-button>刷新</el-button>
      </div>
      <!-- 表格 -->
      <el-table :data="tableData" style="width: 100%">
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column fixed prop="title" label="标题" width="200">
        </el-table-column>
        <el-table-column prop="state" label="状态" width="120">
          <template slot-scope="scope">
            <div class="state-box">
              <span
                :class="scope.row.state === '已发布' ? 'active' : ''"
              ></span>
              {{ scope.row.state }}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="type" label="分类" width="120">
          <template slot-scope="scope">
            <el-tag type="success">{{ scope.row.type }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="tag" label="标签" width="200">
          <template slot-scope="scope">
            <el-tag>{{ scope.row.tag }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="read_num" label="阅读量" width="120">
          <template slot-scope="scope">
            <span class="read_num">{{ scope.row.read_num }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="likes" label="喜欢数" width="120">
          <template slot-scope="scope">
            <span class="likes">{{ scope.row.likes }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="create_time" label="发布时间" width="200">
          <template slot-scope="scope">
            {{ scope.row.create_time | time }}
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="250">
          <template slot-scope="scope">
            <div class="operate">
              <el-button
                type="text"
                size="small"
                @click="handleClick(scope.row)"
                >编辑</el-button
              >
              <el-button type="text" size="small">撤销首焦</el-button>
              <el-button type="text" size="small">查看访问</el-button>
              <el-button
                type="text"
                size="small"
                @click="delArticle(scope.row.id)"
                >删除</el-button
              >
            </div>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        background
        layout="prev, pager, next"
        :total="total"
        @current-change="currentChange"
        :page-size="pageSize"
      >
      </el-pagination>
    </div>
  </div>
</template>
<script>
import dayjs from "dayjs";

export default {
  data() {
    return {
      formInline: {
        title: "",
        state: "",
        type: "",
      },
      tableData: [],
      pageIndex: 1,
      pageSize: 8,
      total: 0,
      classifyList: [],
    };
  },
  created() {
    this.getArticleList();
    this.getClassifyList();
  },
  computed: {},
  filters: {
    time(value) {
      return dayjs(value).format("YYYY-MM-DD hh:mm:ss");
    },
  },
  methods: {
    onSubmit() {
      this.getArticleList();
    },
    // 重置表单
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    // 点击编辑
    handleClick(row) {
      this.$router.push({
        name: "Editor",
        query: { item: row },
      });
    },
    // 获取文章列表
    getArticleList() {
      this.$api
        .getArticleList({
          pageIndex: this.pageIndex,
          pageSize: this.pageSize,
          ...this.formInline,
        })
        .then((res) => {
          this.tableData = res.data.data;
          this.total = res.data.total;
        });
    },
    // 分页页码改变
    currentChange(index) {
      this.pageIndex = index;
      this.getArticleList();
    },
    // 获取分类列表
    getClassifyList() {
      this.$api.getClassifyList().then((res) => {
        this.classifyList = res.data;
      });
    },
    // 删除文章
    delArticle(id) {
      this.$api.delArticle({ id }).then((res) => {
        if (res.success) {
          this.$message({
            message: "删除成功",
            type: "success",
          });
          this.getArticleList();
        }
      });
    },
    // 去编辑器
    toEditor() {
      this.$router.push("/editor");
    },
  },
  components: {},
};
</script>
<style lang="scss" scoped>
.allArticle {
  width: 100%;
  background-color: #eff2f5;
  display: flex;
  flex-direction: column;
  align-items: center;
  .search-allArticle {
    width: 95%;
    height: 150px;
    background-color: #fff;
    margin-top: 20px;
    .el-form {
      display: flex;
      height: 150px;
      justify-content: space-around;
      align-items: center;
      .operate {
        width: 300px;
        height: 80%;
        display: flex;
        justify-content: end;
        align-items: flex-end;
      }
    }
  }
  .table-allArticle {
    width: 95%;
    background-color: #fff;
    margin-top: 20px;
    /*滚动条样式*/
    ::-webkit-scrollbar {
      /* width: 4px; */
      height: 8px;
    }
    ::-webkit-scrollbar-thumb {
      border-radius: 10px;
      background: #dadada;
    }
    ::-webkit-scrollbar-track {
      border-radius: 0;
    }
    .table-header {
      height: 50px;
      display: flex;
      justify-content: flex-end;
      align-items: center;
      .el-button {
        height: 40px;
      }
    }
    .cell {
      span.read_num {
        padding: 5px 8px;
        border-radius: 50%;
        color: #fff;
        background-color: #5bc726;
      }
      span.likes {
        padding: 5px 8px;
        border-radius: 50%;
        color: #fff;
        background-color: #eb2f96;
      }
      .state-box {
        display: flex;
        align-items: center;
        span {
          width: 10px;
          height: 10px;
          background-color: #faad14;
          margin-right: 5px;
          border-radius: 50%;
        }
        span.active {
          background-color: #5bc726;
        }
      }
      .operate {
        display: flex;
        justify-content: space-around;
      }
    }
    .el-pagination {
      display: flex;
      justify-content: end;
    }
  }
}
</style>